package com.wintrue.ffxs.ui;

import android.Manifest;
import android.app.AppOpsManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;

import com.facebook.drawee.view.SimpleDraweeView;
import com.wintrue.ffxs.R;
import com.wintrue.ffxs.base.BaseActivity;
import com.wintrue.ffxs.base.MApplication;
import com.wintrue.ffxs.ui.home.MainActivity;
import com.wintrue.ffxs.utils.ActivityUtil;
import com.wintrue.ffxs.utils.DensityUtil;
import com.wintrue.ffxs.utils.FrescoImageLoader;
import com.wintrue.ffxs.utils.ImageUtil;
import com.wintrue.ffxs.utils.PrefersUtil;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;

import butterknife.Bind;
import butterknife.ButterKnife;

public class SplashActivity extends BaseActivity {

    private final int SPLASH_TIME_DELAY = 3 * 1000; // 延迟
    private long mInitTime;
    @Bind(R.id.test_img)
    SimpleDraweeView imgView;
    @Bind(R.id.test_img_1)
    SimpleDraweeView imgView1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 用于第一次安装APP，进入到除这个启动activity的其他activity，点击home键，再点击桌面启动图标时，
        // 系统会重启此activty，而不是直接打开之前已经打开过的activity，因此需要关闭此activity
        if (!isTaskRoot()) {
            finish();
            return;
        }
        mInitTime = System.currentTimeMillis();// 记录进入时间，页面跳转时间
        setContentView(R.layout.activity_splash);
        ButterKnife.bind(this);
        if (TextUtils.isEmpty(MApplication.getInstance().getDisplay())) {
            String display = DensityUtil.getScreenWidth(this) + "_" + DensityUtil.getScreenHeight(this);
            MApplication.getInstance().setDisplay(display);
        }
        requestPermission(1, new String[]{Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.ACCESS_FINE_LOCATION, WRITE_EXTERNAL_STORAGE}, AppOpsManager.OPSTR_READ_PHONE_STATE, new Runnable() {
            @Override
            public void run() {
                toUpGuide();
            }
        }, new Runnable() {
            @Override
            public void run() {
                showToastMsg("您已拒绝应用所需相关权限，为了您更好的体验，请到应用权限管理中心设置相关权限");
                toUpGuide();
            }
        });
        String url = "http://ww3.sinaimg.cn/large/610dc034jw1f6m4aj83g9j20zk1hcww3.jpg";
        ImageUtil.displayImage(url,imgView);
        FrescoImageLoader.loadImageBlur(imgView1,url);
//        imgView.setImageURI(url);
//        Picasso.with(this).load(url).placeholder(R.drawable.bg_jz).error(R.drawable.bg_jz).into(imgView);
    }

    private void toUpGuide() {
        long finishTime = System.currentTimeMillis();
        if (finishTime - mInitTime > SPLASH_TIME_DELAY) {
            toMain();
        } else {
            showWaitDialog("稍后");
            new Thread(){
                @Override
                public void run() {
                    super.run();
                    try {
                        Thread.sleep(SPLASH_TIME_DELAY);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            hideWaitDialog();
                            toMain();
                        }
                    });
                }
            }.start();
//            new Handler().postDelayed(new Runnable() {

//                @Override
//                public void run() {
//                    toMain();
//                }
//            }, SPLASH_TIME_DELAY - (finishTime - mInitTime));
        }
    }

    private void toMain() {
        if (PrefersUtil.getInstance().getBooleanValue("app_is_first_open", true)) {
//            PrefersUtil.getInstance().setValue("app_is_first_open", false);
            ActivityUtil.next(SplashActivity.this, GuidanceActivity.class, true);
        } else {
            ActivityUtil.next(SplashActivity.this, MainActivity.class, true);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!MApplication.getInstance().isWifiConnectNetWork()) {
            showToastMsg("为了您更好的浏览体验，请切换至wifi状态");
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            MApplication.getInstance().exit();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    protected void onDestroy() {
        ButterKnife.unbind(this);
        super.onDestroy();
    }

}
